package hu.rsc.feeds.client.gui.generated;

import hu.rsc.feeds.client.Feeds;
import hu.rsc.feeds.client.data.ColumnFilterInfo;
import hu.rsc.feeds.client.data.ColumnFilterList;
import hu.rsc.feeds.client.data.Dictionary;
import hu.rsc.feeds.client.data.FieldMapping;
import hu.rsc.feeds.client.data.GeneratedEntry;
import hu.rsc.feeds.client.gui.FeedPanel;
import hu.rsc.feeds.client.gui.table2.AdvancedAbstractColumnDefinition;
import hu.rsc.feeds.client.gui.table2.AdvancedPagingScrollTable;
import hu.rsc.feeds.client.gui.table2.FilterChangeListener;
import hu.rsc.feeds.client.service.RssReaderServiceAsync;

import com.google.gwt.gen2.table.client.DefaultRowRenderer;
import com.google.gwt.gen2.table.client.DefaultTableDefinition;
import com.google.gwt.gen2.table.client.FixedWidthGridBulkRenderer;
import com.google.gwt.gen2.table.client.PagingOptions;
import com.google.gwt.gen2.table.client.PagingScrollTable;
import com.google.gwt.gen2.table.client.ScrollTable;
import com.google.gwt.gen2.table.client.SelectionGrid.SelectionPolicy;
import com.google.gwt.gen2.table.client.TableDefinition;
import com.google.gwt.gen2.table.event.client.RowSelectionEvent;
import com.google.gwt.gen2.table.event.client.RowSelectionHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class GeneratedEntryTable extends Composite implements FilterChangeListener, RowSelectionHandler {

    private AdvancedPagingScrollTable<GeneratedEntry> scrollTable;
    private GeneratedEntryTableModel szamlaTableModel;
    private final ColumnFilterList columnFilterList = new ColumnFilterList();
    private static final RssReaderServiceAsync service = Feeds.getService();
    private final FeedPanel parent;

    public GeneratedEntryTable(FeedPanel parent) {
	this.parent = parent;
	this.szamlaTableModel = new GeneratedEntryTableModel(parent, this);

	TableDefinition<GeneratedEntry> tableDef = this.createTableDefinition();

	// Create the scroll table
	this.scrollTable = new AdvancedPagingScrollTable<GeneratedEntry>(this.szamlaTableModel, tableDef, Dictionary.getEntry(Dictionary.LABEL_TABLE_ENTRY,
		null));
	this.scrollTable.setPageSize(10);
	this.scrollTable.setEmptyTableWidget(new HTML(Dictionary.getEntry(Dictionary.LABEL_EMPTY_TABLE_ENTRY, null)));

	// Setup the bulk renderer
	FixedWidthGridBulkRenderer<GeneratedEntry> bulkRenderer = new FixedWidthGridBulkRenderer<GeneratedEntry>(this.scrollTable.getDataTable(),
		this.scrollTable);
	this.scrollTable.setBulkRenderer(bulkRenderer);

	// Setup the formatting
	this.scrollTable.setCellPadding(3);
	this.scrollTable.setCellSpacing(0);
	this.scrollTable.setResizePolicy(ScrollTable.ResizePolicy.FILL_WIDTH);
	this.scrollTable.getDataTable().setSelectionPolicy(SelectionPolicy.ONE_ROW);
	this.scrollTable.setHeight("320px");

	this.refreshTable();

	this.scrollTable.getDataTable().addRowSelectionHandler(this);

	VerticalPanel tablePanel = new VerticalPanel();

	HorizontalPanel tablePagingPanel = new HorizontalPanel();
	tablePagingPanel.setWidth("400px");
	tablePagingPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
	tablePanel.add(tablePagingPanel);

	tablePanel.setWidth("400px");
	tablePanel.addStyleName("fullSize");
	tablePanel.add(this.scrollTable);

	tablePagingPanel.add(new PagingOptions(getTable()));

	initWidget(tablePanel);
    }

    public void doFilter() {
	this.scrollTable.reloadPage();
    }

    public ColumnFilterList getFilters() {
	return this.columnFilterList;
    }

    public PagingScrollTable<GeneratedEntry> getTable() {
	return this.scrollTable;
    }

    @Override
    public void listenForFilterChange(ColumnFilterInfo info) {
	this.columnFilterList.add(info);
	this.refreshTable();
    }

    @Override
    public void onRowSelection(RowSelectionEvent event) {
	GeneratedEntry feed = null;
	for (Integer index : this.scrollTable.getDataTable().getSelectedRows()) {
	    feed = this.scrollTable.getRowValue(index);
	}
	if (feed == null) {
	    return;
	}
	this.parent.refreshEntryForm(feed);

    }

    public void refreshTable() {
	service.getGeneratedEntriesCount(this.columnFilterList, this.parent.getFeedId(), new AsyncCallback<Integer>() {

	    @Override
	    public void onFailure(Throwable t) {
		GeneratedEntryTable.this.szamlaTableModel.setRowCount(0);
	    }

	    @Override
	    public void onSuccess(Integer count) {
		GeneratedEntryTable.this.szamlaTableModel.setRowCount(count);
		GeneratedEntryTable.this.scrollTable.reloadPage();
	    }
	});
    }

    private TableDefinition<GeneratedEntry> createTableDefinition() {
	// Create the table definition
	DefaultTableDefinition<GeneratedEntry> tableDefinition = new DefaultTableDefinition<GeneratedEntry>();

	// Set the row renderer
	tableDefinition.setRowRenderer(new DefaultRowRenderer<GeneratedEntry>(new String[] { "#FFFFFF", "#EEEEEE" }));

	{
	    AdvancedAbstractColumnDefinition<GeneratedEntry, String> columnDef = new AdvancedAbstractColumnDefinition<GeneratedEntry, String>(
		    Dictionary.getEntry(Dictionary.LABEL_GEN_TITLE, null), FieldMapping.FIELD_ENTRY_TITLE, this.szamlaTableModel, this,
		    AdvancedAbstractColumnDefinition.MODE_SORT) {

		@Override
		public String getCellValue(GeneratedEntry rowValue) {
		    return rowValue.getTitle();
		}

		@Override
		public void setCellValue(GeneratedEntry rowValue, String cellValue) {
		    // rowValue.setSzerzo(cellValue);
		}
	    };
	    columnDef.setMinimumColumnWidth(100);
	    columnDef.setPreferredColumnWidth(100);
	    columnDef.setColumnSortable(true);
	    columnDef.setColumnTruncatable(true);
	    columnDef.setHeader(0, columnDef.getHeaderWidget());
	    tableDefinition.addColumnDefinition(columnDef);
	}

	{
	    AdvancedAbstractColumnDefinition<GeneratedEntry, String> columnDef = new AdvancedAbstractColumnDefinition<GeneratedEntry, String>(
		    Dictionary.getEntry(Dictionary.LABEL_GEN_LINK, null), FieldMapping.FIELD_ENTRY_LINK, this.szamlaTableModel, this,
		    AdvancedAbstractColumnDefinition.MODE_SORT) {

		@Override
		public String getCellValue(GeneratedEntry rowValue) {
		    return rowValue.getLink();
		}

		@Override
		public void setCellValue(GeneratedEntry rowValue, String cellValue) {
		    // rowValue.setSzerzo(cellValue);
		}
	    };
	    columnDef.setMinimumColumnWidth(100);
	    columnDef.setPreferredColumnWidth(100);
	    columnDef.setColumnSortable(true);
	    columnDef.setColumnTruncatable(true);
	    columnDef.setHeader(0, columnDef.getHeaderWidget());
	    tableDefinition.addColumnDefinition(columnDef);
	}

	{
	    AdvancedAbstractColumnDefinition<GeneratedEntry, String> columnDef = new AdvancedAbstractColumnDefinition<GeneratedEntry, String>(
		    Dictionary.getEntry(Dictionary.LABEL_GEN_DATE, null), FieldMapping.FIELD_ENTRY_DATE, this.szamlaTableModel, this,
		    AdvancedAbstractColumnDefinition.MODE_SORT) {

		@Override
		public String getCellValue(GeneratedEntry rowValue) {
		    if (rowValue.getPubdate() == null) {
			return "N/A";
		    }
		    return Feeds.getDateFormat().format(rowValue.getPubdate());
		}

		@Override
		public void setCellValue(GeneratedEntry rowValue, String cellValue) {
		    // rowValue.setSzerzo(cellValue);
		}
	    };
	    columnDef.setMinimumColumnWidth(100);
	    columnDef.setPreferredColumnWidth(100);
	    columnDef.setColumnSortable(true);
	    columnDef.setColumnTruncatable(true);
	    columnDef.setHeader(0, columnDef.getHeaderWidget());
	    tableDefinition.addColumnDefinition(columnDef);
	}

	return tableDefinition;
    }

}
